<template>
  <div class="app-container">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-width="150px"
      class="demo-ruleForm"
      style="width: 800px;"
    >
      <el-form-item label="学校名称" prop="name">
        <el-input v-model="ruleForm.name" />
      </el-form-item>
      <el-form-item label="校徽" prop="name">
        <my-upload v-model="ruleForm.schoolAvatar" />
      </el-form-item>
      <el-form-item label="学校简介" prop="brief">
        <el-input v-model="ruleForm.brief" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          @click="submitForm('ruleForm')"
        >保存</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import MyUpload from '@/components/my-upload/index'
import { getTeacherDetail } from '@/api/basic'

export default {
  components: { MyUpload },
  data() {
    return {
      ruleForm: {},
      rules: {
        name: [
          { required: true, message: '请输入教师姓名', trigger: 'blur' },
          { max: 20, message: '教师姓名不超过20个字符', trigger: 'blur' }
        ],
        idCard: [
          { required: true, message: '请输入教师身份证号', trigger: 'blur' },
          { max: 18, min: 18, message: '请输入正确的18位身份证号', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择教师性别', trigger: 'change' }
        ],
        nation: [
          { required: true, message: '请选择教师民族', trigger: 'change' }
        ],
        telephone: [
          { required: true, pattern: /^1[23456789]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        birth: [
          { required: true, message: '请选择出生日期', trigger: 'change' }
        ],
        party: [
          { required: true, message: '请选择政治面貌', trigger: 'change' }
        ],
        joinTime: [
          { required: true, message: '请选择入职时间', trigger: 'change' }
        ],
        address: [
          { required: true, message: '请输入家庭住址', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    console.log(this.$route)
    const route = this.$route
    if (route.name === 'teacher-edit') {
      this.getDetail(route.params.id)
    }
  },
  methods: {
    async getDetail(id) {
      try {
        const res = await getTeacherDetail({
          id: Number(id)
        })
        if (res) {
          console.log(res)
          this.ruleForm = res
        }
      } catch (err) {
        console.log('接口出错')
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style scoped lang="less">
ul,
li {
  padding: 0;
  margin: 0;
}
li {
  list-style: none;
}
.app-container {
  background: #fff;
}
.match-type-check-area {
  margin-top: 12px;
  .left {
    display: block;
    margin-bottom: 5px;
  }
}
</style>
